package 线程同步.不安全的案例;

public class ToWithdrawMoney {
    public static void main(String[] args) {
        money money = new money("123456",100);
        new Thread(new toMoney("123456", 50,money),"张三").start();
        new Thread(new toMoney("123456", 100,money),"张四").start();
    }
}
class money{
    private String name;
    private long money;
    public money(String name,long money){
        this.money=money;
        this.name=name;
    }

    public long getMoney() {
        return money;
    }

    public void setMoney(long money) {
        this.money = money;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
class toMoney implements Runnable{
    private String name;
    private long tomoney;
    money money;
    public toMoney(String name,long tomoney,money money){
        this.name=name;
        this.tomoney=tomoney;
        this.money=money;
    }

    @Override
    public void run() {
        To();
    }
    private  void To(){
        if (money.getName().equals(name)){
            if (money.getMoney()-tomoney>=0){
                System.out.println("你的余额为："+money.getMoney());
                money.setMoney(money.getMoney()-tomoney);
                System.out.println(Thread.currentThread().getName()+"-->取了"+tomoney);
                System.out.println("你的余额为："+money.getMoney());
            }else {
                System.out.println("你的钱不够");
            }
        }else {
            System.out.println("你的账号错误");
        }
    }
}